<template>
  <div>
    <el-dialog title="临时用户收费标准" :visible.sync="show" width="500px" append-to-body>
      <el-form :model="form" ref="form" :rules="rules" v-loading="loading">
        <h3>充电</h3>
        <el-form-item prop="chargeHourAmount">
          <el-input
            v-model="form.chargeHourAmount"
            placeholder="请输入金额"
            type="number"
            :max="99999"
            :min="0"
          >
            <span slot="suffix">元/小时</span>
          </el-input>
        </el-form-item>
        <el-form-item prop="chargeDayAmount">
          <el-input
            v-model="form.chargeDayAmount"
            placeholder="请输入金额"
            type="number"
            :max="99999"
            :min="0"
          >
            <span slot="suffix">元/天</span>
          </el-input>
        </el-form-item>
        <h3>停车</h3>
        <el-form-item prop="stopHourAmount">
          <el-input
            v-model="form.stopHourAmount"
            placeholder="请输入金额"
            type="number"
            :max="99999"
            :min="0"
          >
            <span slot="suffix">元/小时</span>
          </el-input>
        </el-form-item>
        <el-form-item prop="stopDayAmount">
          <el-input
            v-model="form.stopDayAmount"
            placeholder="请输入金额"
            type="number"
            :max="99999"
            :min="0"
          >
            <span slot="suffix">元/天</span>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSumbit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTempPrice } from "@/api/weiriya/vipSetting";
import { updateNonVipSetting } from "@/api/weiriya/nonVipSetting";
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    // 停车点位ID
    id: {
      type: String,
      default: "",
    },
  },
  computed: {
    show: {
      get() {
        return this.value;
      },
      set() {
        this.$emit("input", false);
      },
    },
  },
  watch: {
    value(val) {
      if (val) {
        this.getData();
      }
    },
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false, // 加载状态
      form: {},
      rules: {
        // 验证规则
        chargeHourAmount: [{ required: true, message: "请输入金额", trigger: "blur" }],
        chargeDayAmount: [{ required: true, message: "请输入金额", trigger: "blur" }],
        stopHourAmount: [{ required: true, message: "请输入金额", trigger: "blur" }],
        stopDayAmount: [{ required: true, message: "请输入金额", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 获取数据
    getData() {
      this.loading = true;
      getTempPrice(this.id)
        .then((res) => {
          this.form = res.data;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 提交表单
    handleSumbit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          updateNonVipSetting(this.form)
            .then((res) => {
              this.$message.success("修改成功");
              this.$emit("success");
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
